//
//  VSTreeViewController.m
//  ViewSeer
//
//  Created by LevinYan on 2017/3/21.
//  Copyright © 2017年 LevinYan. All rights reserved.
//

#import "VSTreeViewController.h"
#import <RATreeView/RATreeView.h>
#import "VSTreeViewCell.h"



@interface VSTreeViewController ()<RATreeViewDataSource, RATreeViewDelegate>

@property (nonatomic, strong) RATreeView *treeView;

@end

@implementation VSTreeViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

    self.treeView = [[RATreeView alloc] initWithFrame:self.view.bounds style:RATreeViewStylePlain];
    self.treeView.dataSource = self;
    self.treeView.delegate = self;
    self.treeView.separatorColor = [UIColor blackColor];
    self.treeView.scrollView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    self.treeView.treeFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.treeView];
}

- (void)viewDidLayoutSubviews
{
    self.treeView.frame = self.view.bounds;
}
- (void)setRootNode:(VSNode *)rootNode
{
    _rootNode = rootNode;
    [self.treeView reloadData];
}
- (void)setSelectedNode:(VSNode *)node
{
    [self.treeView selectRowForItem:node animated:YES scrollPosition:RATreeViewScrollPositionMiddle];
}


#pragma mark TreeView Delegate

- (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(VSNode*)item
{
    return 44;
}
- (void)treeView:(RATreeView *)treeView didSelectRowForItem:(VSNode*)item
{
    if([self.delegate respondsToSelector:@selector(treeViewControllerDidSelectNode:)]){
        [self.delegate treeViewControllerDidSelectNode:item];
    }
}
- (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item
{
    VSTreeViewCell *cell = (VSTreeViewCell*)[treeView cellForItem:item];
    [cell expandedAnimated:YES];
}
- (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item
{
    VSTreeViewCell *cell = (VSTreeViewCell*)[treeView cellForItem:item];
    [cell expandedAnimated:NO];

}

#pragma mark RATreeViewDataSource
- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(nullable VSNode*)item
{
    if(!item)
        return self.rootNode.subnodes.count;
    else
        return item.subnodes.count;
}


- (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(nullable VSNode*)item
{
    
    VSTreeViewCell *cell = [treeView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[VSTreeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        

    }
    
    VSTreeViewCellModel *model = [VSTreeViewCellModel new];
    model.name = item.name;
    model.level = [treeView levelForCellForItem:item];
    model.expandedShow = item.subnodes.count;
    model.expanded = [treeView isCellForItemExpanded:item];
    [cell updateUI:model];
    
    return cell;
}


- (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(nullable VSNode*)item
{
    if(!item)
        return self.rootNode.subnodes[index];
    else
        return item.subnodes[index];
}


@end
